package com.wuhao.shootclient.Receiver;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class BleReceiver extends BroadcastReceiver {

    private BluetoothAdapter mBluetoothAdapter;

    private List<HashMap> blueList;

    private HashMap blueHashMap;

    @Override
    public void onReceive(Context context, Intent intent) {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        int test =  mBluetoothAdapter.getState();

        switch (test) {
            //关闭
            case BluetoothAdapter.STATE_OFF:
                openBle();
                break;
            //打开
            case BluetoothAdapter.STATE_ON:
                break;
        }
    }

    private void openBle() {
        if (mBluetoothAdapter!=null){
            if (mBluetoothAdapter.isEnabled()){
                try {
                    mBluetoothAdapter.enable();
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();
            blueList = new ArrayList<HashMap>();
//            Log.d(TAG,"获取已经配对devices"+devices.size());
            for (BluetoothDevice bluetoothDevice : devices)
            {
//                Log.d(TAG, "已经配对的蓝牙设备：");
//                Log.d(TAG, bluetoothDevice.getName());
//                Log.d(TAG, bluetoothDevice.getAddress());
                blueHashMap = new HashMap();
                blueHashMap.put("blue_device",bluetoothDevice);
                blueHashMap.put("blue_name",bluetoothDevice.getName());
                blueHashMap.put("blue_address",bluetoothDevice.getAddress());
                blueList.add(blueHashMap);
            }

        }
    }
}
